package com.myfoon.action.backing.context;

import com.myfoon.action.backing.system.UserBean;
import com.myfoon.model.entity.Country;
import com.myfoon.model.entity.User;
import com.myfoon.utils.CalendarUtils;
import com.myfoon.utils.CountryUtils;
import com.myfoon.view.context.helper.BeanReceiver;

import java.sql.Date;

import java.util.Calendar;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.persistence.EntityExistsException;

import javax.servlet.http.HttpSession;


public class RegistrationBean {
    private User user;    

    private int currentDay;
    private int currentMonth;
    private int currentYear;

    private int currentCountry;

    public final String sessionKey = "fooncaptcha";
    private String captchaKey;
    
    private boolean signup1rendered;
    private boolean signup2rendered;        

    public RegistrationBean() {
        user = new User();
        user.getUserPi().setGender(false);        

        currentDay = 0;
        currentMonth = 0;
        currentYear = 0;
        currentCountry = 0;    
        signup1rendered = true;
        signup2rendered = false;
    }
            
    public boolean isValidCaptcha() {
        String captchaValue = (String)((HttpSession) FacesContext
                        .getCurrentInstance().getExternalContext().getSession(true))
                        .getAttribute(sessionKey);        

        if (!captchaValue.equalsIgnoreCase(captchaKey)) {
            return false;
        }        
        return true;
    }
    
    public boolean isValidEmail() {
        return true;
    }
    public boolean isValidUsername() {
        return true;
    }    
    public boolean isValidPassword() {
        return true;
    }    
    public boolean isValidName() {
        return true;
    }
    
    public boolean isValidGender() {
        return true;
    }
    
    public boolean isValidCountry() {
        return true;
    }
    
    public boolean isValidDateOfBirth() {
        return true;
    }
    
    /*
    public void validate(ActionEvent event) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExtendedRenderKitService erks = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);        
        
        System.out.println("hello");
        
        if (usr.getEmail().equalsIgnoreCase("onur@onur.com")) {
            erks.addScript(facesContext, "showHintBox('E-mail you selected is already exists','error','1');");
            System.out.println("hello 1");
        }            
        else {            
            signup1rendered = false;
            signup2rendered = true;
            System.out.println("hello 2");
        }                        
    }*/
    
    
    public String validate() {
        
        String result = "failed";             
        /*        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        //ExtendedRenderKitService erks = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
        
        if (user.getEmail().equalsIgnoreCase("onur@onur.com")) {
            erks.addScript(facesContext, "showHintBox('E-mail you selected is already exists','error','1');");            
        }            
        else {     
            setSignup1rendered(false);
            setSignup2rendered(true);            
        }                        
        */
        return result;
    }
    
    public boolean validateEmail() {
        return true;
    }
    
    public boolean validateAll() {
        if (!isValidEmail()) {                    
            return false;
        }
        if (!isValidPassword()) {
            return false;
        }
        
        if (!isValidName()) {
            return false;
        }
        return true;
    }
    
    public String register() {        
        String result = "pretty:";   
                
        if (!isValidCaptcha()) {            
            return result;
        }        
        if (validateAll() == true) {
            
        }
        
        UserBean userBean = new UserBean();
        Country userCountry = new Country();
        userCountry.setId(currentCountry);                       
        
        user.getUserPi().setCountry(userCountry);
        user.getUserPi().setBirthdate(CalendarUtils.createDate(currentYear, currentMonth, currentDay+1));
        
        try {
            userBean.persist(user);    
        }
        catch(EntityExistsException ex) {
            ex.printStackTrace();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
                                                    
        LoginBean loginBean = (LoginBean)BeanReceiver.receiveBean("LoginBean");        
        loginBean.setUser(user);        
        loginBean.setLoggedIn(true);
        
        result = "pretty:home";
        return result;
    }
    
    public List<SelectItem> getMonthList() {        
        return null;//CalendarUtils.getMonthList();
    }

    public List<SelectItem> getDayList() {
        return null;//CalendarUtils.getDayList();
    }

    public List<SelectItem> getYearList() {
        return null;//CalendarUtils.getYearList();
    }

    public List<SelectItem> getCountryList() {        
        return null;//new CountryUtils().getCountryList();
    }

    public void setCaptchaKey(String captchaKey) {
        this.captchaKey = captchaKey;
    }

    public String getCaptchaKey() {
        return captchaKey;
    }

    public void setCurrentDay(int currentDay) {
        this.currentDay = currentDay;
    }

    public int getCurrentDay() {
        return currentDay;
    }

    public void setCurrentMonth(int currentMonth) {
        this.currentMonth = currentMonth;
    }

    public int getCurrentMonth() {
        return currentMonth;
    }

    public void setCurrentYear(int currentYear) {
        this.currentYear = currentYear;
    }

    public int getCurrentYear() {
        return currentYear;
    }

    public void setCurrentCountry(int currentCountry) {
        this.currentCountry = currentCountry;
    }

    public int getCurrentCountry() {
        return currentCountry;
    }

    public String getSessionKey() {
        return sessionKey;
    }

    public void setSignup1rendered(boolean signup1rendered) {
        this.signup1rendered = signup1rendered;
    }

    public boolean isSignup1rendered() {
        return signup1rendered;
    }

    public void setSignup2rendered(boolean signup2rendered) {
        this.signup2rendered = signup2rendered;
    }

    public boolean isSignup2rendered() {
        return signup2rendered;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }
}
